package com.ruoyi.project.newhope.materialInspection.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.newhope.materialInspection.domain.NewHopeMaterialInspection;
import com.ruoyi.project.newhope.materialInspection.service.INewHopeMaterialInspectionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 物料检测项目设置(中间[物料主数据设置-化验项目设置])Controller
 * 
 * @author ruoyi
 * @date 2021-02-08
 */
@Controller
@RequestMapping("/api/newhope/materialInspection")
public class NewHopeMaterialInspectionController extends BaseController
{

    @Autowired
    private INewHopeMaterialInspectionService newHopeMaterialInspectionService;

    /**
     * 查询物料检测项目设置(中间[物料主数据设置-化验项目设置])列表
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(NewHopeMaterialInspection newHopeMaterialInspection)
    {
        startPage();
        List<NewHopeMaterialInspection> list = newHopeMaterialInspectionService.selectNewHopeMaterialInspectionList(newHopeMaterialInspection);
        return getDataTable(list);
    }

    /**
     * 导出物料检测项目设置(中间[物料主数据设置-化验项目设置])列表
     */
    @Log(title = "物料检测项目设置(中间[物料主数据设置-化验项目设置])", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestBody NewHopeMaterialInspection newHopeMaterialInspection)
    {
        List<NewHopeMaterialInspection> list = newHopeMaterialInspectionService.selectNewHopeMaterialInspectionList(newHopeMaterialInspection);
        ExcelUtil<NewHopeMaterialInspection> util = new ExcelUtil<NewHopeMaterialInspection>(NewHopeMaterialInspection.class);
        return util.exportExcel(list, "materialInspection");
    }

    /**
     * 新增保存物料检测项目设置(中间[物料主数据设置-化验项目设置])
     */
    @Log(title = "物料检测项目设置(中间[物料主数据设置-化验项目设置])", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody NewHopeMaterialInspection newHopeMaterialInspection)
    {
        if (newHopeMaterialInspection.getMaterialId() == null) {
            return AjaxResult.error("物料不能为空");
        }
        if (newHopeMaterialInspection.getInspectionId() == null) {
            return AjaxResult.error("检测项目不能为空");
        }
        if (newHopeMaterialInspection.getMax() == null) {
            return AjaxResult.error("最大值不能为空");
        }
        if (newHopeMaterialInspection.getMin() == null) {
            return AjaxResult.error("最小值不能为空");
        }
        Integer checkFlag = newHopeMaterialInspectionService.isExist(newHopeMaterialInspection);
        if (checkFlag > 0) {
            return AjaxResult.error("当前物料和检测项目已经存在，请勿重复添加!");
        }
        return toAjax(newHopeMaterialInspectionService.insertNewHopeMaterialInspection(newHopeMaterialInspection));
    }

    /**
     * 修改物料检测项目设置(中间[物料主数据设置-化验项目设置])
     */
    @GetMapping("/edit/{id}")
    @ResponseBody
    public NewHopeMaterialInspection edit(@PathVariable("id") Long id)
    {
        NewHopeMaterialInspection newHopeMaterialInspection = newHopeMaterialInspectionService.selectNewHopeMaterialInspectionById(id);
        return newHopeMaterialInspection;
    }

    /**
     * 修改保存物料检测项目设置(中间[物料主数据设置-化验项目设置])
     */
    @Log(title = "物料检测项目设置(中间[物料主数据设置-化验项目设置])", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody NewHopeMaterialInspection newHopeMaterialInspection)
    {
        if (newHopeMaterialInspection.getMaterialId() == null) {
            return AjaxResult.error("物料不能为空");
        }
        if (newHopeMaterialInspection.getInspectionId() == null) {
            return AjaxResult.error("检测项目不能为空");
        }
        if (newHopeMaterialInspection.getMax() == null) {
            return AjaxResult.error("最大值不能为空");
        }
        if (newHopeMaterialInspection.getMin() == null) {
            return AjaxResult.error("最小值不能为空");
        }
        Integer checkFlag = newHopeMaterialInspectionService.isExist(newHopeMaterialInspection);
        if (checkFlag > 1) {
            return AjaxResult.error("当前物料和检测项目已经存在，请勿重复添加!");
        }
        return toAjax(newHopeMaterialInspectionService.updateNewHopeMaterialInspection(newHopeMaterialInspection));
    }

    /**
     * 删除物料检测项目设置(中间[物料主数据设置-化验项目设置])
     */
    @Log(title = "物料检测项目设置(中间[物料主数据设置-化验项目设置])", businessType = BusinessType.DELETE)
    @GetMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        return toAjax(newHopeMaterialInspectionService.deleteNewHopeMaterialInspectionByIds(id));
    }
}
